from typing import *


class Solution:

    def longestAlternatingSubarray(self, nums: List[int],
                                   threshold: int) -> int:
        ans, l, r = 0, -1, 0
        while r < len(nums):

            if nums[r] > threshold or l != -1 and nums[r] % 2 == nums[r - 1] % 2:
                l = -1

            if l == -1 and nums[r] % 2 == 0 and nums[r] <= threshold:
                l = r

            if l != -1 and nums[r] <= threshold and (r == l or nums[r] % 2 != nums[r - 1] % 2):
                ans = max(ans, r - l + 1)

            r += 1
        return ans